package com.onegene.pdf.component.event;

import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.VerticalAlignment;
import lombok.Getter;

import java.util.Set;

/**
 * @author laoliangliang
 * @since 2020-05-24 13:53
 */
public class DrugPageNumberEvent implements IEventHandler {

    private Document doc;
    @Getter
    private int pageNum = 0;
    private Set<Integer> noPageSet;

    public DrugPageNumberEvent(Document doc, Set<Integer> noPageSet) {
        this.doc = doc;
        this.noPageSet = noPageSet;
    }

    @Override
    public void handleEvent(Event event) {
        PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
        PdfDocument pdfDoc = docEvent.getDocument();
        PageSize pageSize = pdfDoc.getDefaultPageSize();
        pageNum++;
        int pageNumber = pdfDoc.getNumberOfPages();
        if (!noPageSet.contains(pageNumber)) {
            doc.showTextAligned(new Paragraph(String.format("- %d -", pageNum)), pageSize.getWidth() / 2, 42, pageNumber, TextAlignment.CENTER, VerticalAlignment.MIDDLE, 0);
        }
    }
}
